package thread.processing.performance;

import java.util.concurrent.TimeUnit;

public class WithThread2 {
	static int counter = 0;
	
	public static void main(String[] args) {
		
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
					Util.doWork();
					counter++;
			}
		};
		
		
		long startTime = System.nanoTime();
		
		// start 10 threads
		Thread[] thread = new Thread[100];
		for (int i=0; i < 100; i++){
			thread[i] = new Thread(runnable);
			thread[i].start();
		}
		
		// just waits for each of them to finish
		for(Thread t: thread){
			try {
			t.join();
			}catch(InterruptedException ex){
				System.out.println(ex);
			}
		}
		long elapsedTime = System.nanoTime() - startTime;
		long seconds = TimeUnit.SECONDS.convert(elapsedTime,
				TimeUnit.NANOSECONDS);
		System.out.println("Time taken : " + seconds + " Seconds");
		
		System.out.println("Completed Tries : " + counter);
		
	}
}

// first run
/*
Time taken : 21 Seconds
Completed Tries : 10
*/